﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ZaidimasSandelys.CustomDrawTools;
using ZaidimasSandelys.Models.Classes;
using ZaidimasSandelys.Models.DataStructures;
using ZaidimasSandelys.Models.Interfaces;
using Ninject;
using System.Reflection;
using System.Configuration;

namespace ZaidimasSandelys
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        IMainWindowModel _model;
        ICurrentGame _game;
        private System.Windows.Media.MediaPlayer _player = null;
        

        [Inject]
        public MainWindow(IMainWindowModel model, ICurrentGame game)
        {
            this._model = model;
            this._game = game;

#if DEBUG
            Debug.Assert(Object.ReferenceEquals(this._model.AppData, this._game.AppData));
#endif
            InitializeComponent();
        }

        protected void OnMainWindowLoaded(object sender, RoutedEventArgs e)
        {
            this.MainLayoutPanel.DataContext = _model;
            _model.GameComplexity = GameLevel.Unassigned;
            _model.IsStarted = false;
            _game.LoadCompleted += _game_LoadCompleted;
        }

        protected void SelectionChangedEventHandler(object sender, SelectionChangedEventArgs args)
        {
#if DEBUG
            var x = args.AddedItems;
            GameLevel gameComplexity = _model.GameComplexity;
#endif
            _model.LoadGameVariants(this.VariantComboBox);
            _model.NotifyPropertyChanged("StartButtonIsEnabled");
            ClearActiveGame();
            _model.IsStarted = false;
        }
        protected void VariantChangedEventHandler(object sender, SelectionChangedEventArgs args)
        {
            ClearActiveGame();
            _model.IsStarted = false;
        }

        protected void StartButtonClick(object sender, RoutedEventArgs e)
        {
            bool oldStarted = _model.IsStarted;
            _model.IsStarted = !oldStarted;
            ClearActiveGame();
            MovementsCount.Focus();

            if (_model.IsStarted)
            {
                MainMenuExpander.IsExpanded = false;
                _model.MovementCount = 0;
                this.StatusLabel.Visibility = System.Windows.Visibility.Hidden;
            }
            _game.LoadGame((short)_model.GameComplexity, _model.GameVariant, this.drawingCanvas);
        }

        private void _game_LoadCompleted(object sender, LoadCompletedEventArgs e)
        {
            _model.Person = e.Person;
            _model.Person.AllGameElements = this.drawingCanvas;
#if DEBUG
            Debug.Assert(_model.Person != null);
            this.Dispatcher.VerifyAccess();
#endif
            this.drawingCanvas.DrawContent();
        }

        protected void WindowPreviewKeyDownHandler(object sender, KeyEventArgs e)
        {
            PersonOrientation? newDirection;

            switch (e.Key)
            {
                case Key.Up:
                    newDirection = PersonOrientation.Top;
                    break;
                case Key.Left:
                    newDirection = PersonOrientation.Left;
                    break;
                case Key.Down:
                    newDirection = PersonOrientation.Down;
                    break;
                case Key.Right:
                    newDirection = PersonOrientation.Right;
                    break;
                default:
                    newDirection = null;
                    break;
            }
            if (_model != null && _model.Person != null && newDirection.HasValue)
            {
                if (_model.Person.ProcessKeyDown(newDirection.Value))
                    this._model.MovementCount++;
            }
            if (newDirection != null)
                e.Handled = true;
        }

        private void ClearActiveGame()
        {
            if (_model != null)
            {
                _model.Person = null;
                _model.MovementCount = 0;
            }
            if (_game != null)
                _game.ClearGame();
            
            if (_player != null)
            {
                _player.Close();
                _player = null;
            }
        }

        protected void onGameOver(object sender, RoutedEventArgs e)
        {
            this.StatusLabel.Visibility = System.Windows.Visibility.Visible;
            _model.IsStarted = false;
            StartMusic();

            


            e.Handled = true;
        }

        private void StartMusic()
        {
            if (_player == null)
                _player = new System.Windows.Media.MediaPlayer();

            var sb = new StringBuilder(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
            sb.AppendFormat(@"\Resources\Music\{0}", _model.Mp3FileName);
            _player.Open(new System.Uri(sb.ToString(), UriKind.Absolute));
            _player.Play();
        }

        private void CanMoveCarBackExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = _model.Person.CanMoveCarBackExecute();
            e.Handled = true;
        }

        private void OnMoveCarBackExecute(object sender, ExecutedRoutedEventArgs e)
        {
            _model.Person.MoveAutocarBack();
            e.Handled = true;
        }

    }
}
